package com.zx.erp.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zx.erp.sys.bean.Dept;
import com.zx.erp.sys.bean.Module;
import com.zx.erp.sys.bean.Province;
import com.zx.erp.sys.bean.Users;
import com.zx.erp.sys.service.DeptService;
import com.zx.erp.sys.service.ModuleService;
import com.zx.erp.sys.service.ProvinceService;
import com.zx.erp.sys.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 作者信息
 * @since 2021-11-10
 */
@Controller
@RequestMapping("/sys/users")
@SessionAttributes({"dlist", "plist", "parentModules", "childModules", "pageInfo"})
public class UsersController {
    @Autowired
    private UsersService ubiz;
    @Autowired
    private ModuleService mbiz;
    @Autowired
    private DeptService dbiz;
    @Autowired
    private ProvinceService pbiz;

    /**
     * 登录功能实现
     *
     * @param uname
     * @param upassword
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public String login(String uname, String upassword, Model model, HttpSession session) {
        Users users = ubiz.login(uname, upassword);
        //从session中获取验证码

        if (users != null) {
            if (users.getStatusId() == 2) {
                model.addAttribute("msg", "用户被禁用！");
                return "forward:/login.html";
            }
            session.setAttribute("users", users);
            //查询所有部门信息
            List<Dept> dlist = dbiz.list();
            model.addAttribute("dlist", dlist);
            //查询所有省份
            List<Province> plist = pbiz.list();
            model.addAttribute("plist", plist);
            //左侧边栏查詢所有启用的父模块
            List<Module> parentModule = mbiz.findAllParentModule(users.getJobId());
            model.addAttribute("parentModules", parentModule);
            //左侧边栏查詢所有启用的模块
            List<Module> childModules = mbiz.selectAllModule(users.getJobId());
            model.addAttribute("childModules", childModules);
            return "redirect:/main.html";
        } else {
            users = new Users();
            session.setAttribute("users", users);
            model.addAttribute("msg", "用户名或密码错误！");
            return "forward:/login.html";
        }


    }

    /**
     * 登出操作
     *
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
//        session.removeAttribute("users");//清除指定的users
        session.invalidate();//清除所有session
        return "redirect:/login.html";
    }

    /**
     * 查詢所有用戶信息
     *
     * @param model
     * @param users
     * @param pageNo
     * @return
     */
    @RequestMapping("/findByPage")
    public String findByPage(Model model, Users users, @RequestParam(value = "pageno", defaultValue = "1") Integer
            pageNo) {
        Integer pageSize = 6;
        Page<Users> pageInfo = ubiz.findAll(users, pageNo, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("user", users);
        //查询所有部门信息
        List<Dept> dlist = dbiz.list();
        model.addAttribute("dlist", dlist);
        return "forward:/sys/users/userList.html";
    }
}

